package cn.tedu.ivos2.application.service.Impl;

import cn.tedu.ivos2.application.mapper.ApplicationMapper;
import cn.tedu.ivos2.application.pojo.dto.ApplicationQuery;
import cn.tedu.ivos2.application.pojo.dto.ApplicationSaveDTO;
import cn.tedu.ivos2.application.pojo.entity.Application;
import cn.tedu.ivos2.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos2.application.service.ApplicationService;
import cn.tedu.ivos2.audit.mapper.AuditMapper;
import cn.tedu.ivos2.audit.pojo.entity.Audit;
import cn.tedu.ivos2.audit.pojo.vo.AuditVO;
import cn.tedu.ivos2.audit.service.AuditService;
import cn.tedu.ivos2.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos2.user.mapper.UserMapper;
import cn.tedu.ivos2.user.pojo.vo.UserLoginVO;
import cn.tedu.ivos2.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos2.vehicle.pojo.entity.Vehicle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringJoiner;
@Slf4j
@Service
@Transactional
public class ApplicationServiceImpl implements ApplicationService {
    @Autowired
    private ApplicationMapper applicationMapper;
    @Autowired
    private AuditService auditService;
    @Autowired
    private AuditMapper auditMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private VehicleMapper vehicleMapper;

    @Override
    public void save(ApplicationSaveDTO applicationSaveDTO) {
        Application application=new Application();
        BeanUtils.copyProperties(applicationSaveDTO,application);

        if (application.getId()==null){
            //向申请表添加数据
            application.setCreateTime(new Date());
            application.setStatus(ApplicationStatusEnum.PENDING.getCode());
            applicationMapper.insert(application);
            //向审核表插入数据
            auditService.saveAudit(application);

        }else {
            //撤销申请
        }

    }

    @Override
    public List<ApplicationVO> select(ApplicationQuery applicationQuery) {
        List<ApplicationVO>applicationVOList= applicationMapper.queryApp(applicationQuery);
        //封装id列表,昵称数据
        for (int i=0;i<applicationVOList.size();i++){
            ApplicationVO applicationVO=applicationVOList.get(i);
            assignAudiUserList(applicationVO);
        }
        return applicationVOList;
    }

    @Override
    public void cancel(Long id) {
        Application application=new Application();
        application.setId(id);
        application.setStatus(ApplicationStatusEnum.CANCEL.getCode());
        applicationMapper.update(application);
        //删除对应的审批单
        auditMapper.deleteByApplicationId(id);
    }

    @Override
    public void distribute(Long applicationId, Long vehicleId) {
        //更新申请表状态
        Application application=new Application();
        application.setId(applicationId);
        application.setVehicleId(vehicleId);
        application.setStatus(ApplicationStatusEnum.ALLOCATION.getCode());
        log.debug("更新申请单车辆数据:{}",application);
        applicationMapper.update(application);
        //更新车辆表状态
        Vehicle vehicle=new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("2");
        log.debug("更新车辆表:{}",vehicle);
        vehicleMapper.update(vehicle);
    }

    @Override
    public void back(Long applicationId, Long vehicleId) {
        //更新申请表状态
        Application application=new Application();
        application.setId(applicationId);
        application.setVehicleId(vehicleId);
        application.setStatus(ApplicationStatusEnum.END.getCode());
        log.debug("更新申请表,还车:{}",application);
        applicationMapper.update(application);
        //更新车辆表状态
        Vehicle vehicle=new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("1");
        log.debug("更新车辆表,还车:{}",vehicle);
        vehicleMapper.update(vehicle);
    }

    /**
     * 为数据库查出来的application对象补充id列表,昵称数据
     * @param applicationVO
     */
    private  void assignAudiUserList(ApplicationVO applicationVO){
        List<String>auditUsernameList=new ArrayList<>();
        List<Long> auditUserIdList=new ArrayList<>();
        List<AuditVO>auditVOList= auditMapper.selectAuditByApplicationId(applicationVO.getId());
        for (int i=0;i<auditVOList.size();i++){
            //获取审批人的id
            AuditVO auditVO=auditVOList.get(i);
            Long id=auditVO.getAuditUserId();
            auditUserIdList.add(id);
            //根据id获取用户名
            UserLoginVO userLoginVO = userMapper.selectById(id);
            auditUsernameList.add(userLoginVO.getUsername());
        }

        applicationVO.setAuditUserIdList(auditUserIdList);
        //把用户名集合转成字符串
        StringJoiner stringJoiner=new StringJoiner(",");
        for (String username:auditUsernameList){
            stringJoiner.add(username);
        }
        applicationVO.setAuditUsernameList(stringJoiner.toString());

    }
}
